package ua.soso.ui.dwr.extension;

import org.directwebremoting.impl.DwrXmlConfigurator;
import org.directwebremoting.util.LocalUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

import java.io.InputStream;

public class SpringDwrXmlConfigurator extends DwrXmlConfigurator implements InitializingBean {
    private Resource dwrXml;

    @Required
    public void setDwrXml(Resource dwrXml) {
        this.dwrXml = dwrXml;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(dwrXml, "Property dwrXml is required");

        InputStream in = dwrXml.getInputStream();

        Assert.notNull(in, "Cannot obtain input stream from resource: " + dwrXml);

        try {
            setInputStream(in);
        } finally {
            LocalUtil.close(in);
        }
    }
}
